﻿using System;
using System.Diagnostics;
using System.Windows;
using AutoLoL.Dialogs;
using AutoLoL.Logic.Automation;
using Microsoft.VisualBasic.ApplicationServices;

namespace AutoLoL.SingleInstance
{
    public class EntryPoint
    {
        [STAThread]
        public static void Main(string[] args)
        {
            SingleInstanceManager manager = new SingleInstanceManager();
            manager.Run(args);
        }
    }

    public class SingleInstanceManager : WindowsFormsApplicationBase
    {
        private SingleInstanceApplication _app;

        public SingleInstanceManager()
        {
            IsSingleInstance = true;
        }

        protected override bool OnStartup(Microsoft.VisualBasic.ApplicationServices.StartupEventArgs e)
        {
            // First time app is launched
            _app = new SingleInstanceApplication();
            _app.Run();
            return false;
        }

        protected override void OnStartupNextInstance(Microsoft.VisualBasic.ApplicationServices.StartupNextInstanceEventArgs eventArgs)
        {
            // Subsequent launches
            base.OnStartupNextInstance(eventArgs);
            _app.Activate();
        }
    }

    public class SingleInstanceApplication : Application
    {
        protected override void OnStartup(System.Windows.StartupEventArgs e)
        {
            base.OnStartup(e);

            // Load theme
            // mercury, candy, edge, frog, inc, metal, sleek
            //Uri themeUri = new Uri("ReuxablesLegacy;component/mercury.xaml", UriKind.Relative);
            //ResourceDictionary themeResource = Application.LoadComponent(themeUri) as ResourceDictionary;

            Uri notificationUri = new Uri("AutoLoL;component/Controls/NotificationDictionary.xaml", UriKind.Relative);
            ResourceDictionary notificationResource = Application.LoadComponent(notificationUri) as ResourceDictionary;

            //Application.Current.Resources.MergedDictionaries.Add(themeResource);
            Application.Current.Resources.MergedDictionaries.Add(notificationResource);

            // Global exception handler
            Application.Current.DispatcherUnhandledException += new System.Windows.Threading.DispatcherUnhandledExceptionEventHandler(Current_DispatcherUnhandledException);

            // Create and show the application's main window
            MainWindow window = new MainWindow();
            window.Show();
            Activate();
        }

        private void Current_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
        {
            ExceptionDialog dialog = new ExceptionDialog(e.Exception);
            dialog.ShowDialog();

            try
            {
                Application.Current.Shutdown();
                e.Handled = true;
            }
            catch { if (Debugger.IsAttached) throw; }
        }

        public void Activate()
        {
            this.MainWindow.Activate();
        }

        protected override void OnExit(ExitEventArgs e)
        {
            AutoChat.StopListening();
            base.OnExit(e);
        }
    }
}